﻿-- =============================================
-- Author:		gsj
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Receipt_Create_G]
	(
	@CompanyId int,
	@CompanyName nvarchar(200),
	@CusName  nvarchar(500),
	@OrderId uniqueidentifier,
	@NOE_Flag int,
	@Je  decimal(18,2),
	@RDate datetime,
	@Memo nvarchar(3000),
	@LUserName varchar(20),
	@LEName nvarchar(50),
	@RItem nvarchar(300),
	@RID varchar(50),
	@SEName nvarchar(50),
	@SUserName varchar(20),
	@PaymentTypeId int,
	@PaymentTypeName nvarchar(50),
	@BankId int,
	@BankName nvarchar(300),
	@GroupFlag varchar(50)
	)
AS
BEGIN

	BEGIN TRANSACTION--开始事务
    DECLARE @errorSun INT --定义错误计数器
    SET @errorSun=0 --没错为0



	INSERT INTO [Receipt]
           ([CompanyId]
           ,[CompanyName]
           ,[CusName]
           ,[OrderId]
           ,[NOE_Flag]
           ,[Je]
           ,[RDate]
           ,[Memo]
           ,[LDate]
           ,[LUserName]
           ,[LEName]
           ,[Flag],
			
			RItem ,
			RID ,
			SEName ,
			SUserName ,
			PaymentTypeId,
			PaymentTypeName,
			BankId ,
			BankName  
           ,GroupFlag
           
           
         )
     VALUES
           (@CompanyId
           ,@CompanyName
           ,@CusName
           ,@OrderId
           ,@NOE_Flag
           ,@Je
           ,@RDate
           ,@Memo
           ,getdate()
           ,@LUserName
           ,@LEName
           ,0
            ,
			@RItem ,
			@RID ,
			@SEName ,
			@SUserName ,
			@PaymentTypeId ,
			@PaymentTypeName,
			@BankId ,
			@BankName   
			,@GroupFlag         
         )
         
        if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		end
	
	update [order] set RCount=RCount+1 where Oid=@OrderId
	
	    if @@ROWCOUNT<1
		begin
			set @errorSun=@errorSun+1		
			goto e;
		end
	
	
	 e:
    PRINT @errorSun
	if @errorSun>0
	begin
		rollback TRANSACTION
	end
	else
	begin

		commit TRANSACTION

	END
	
END
